package com.gec.system.filter;

import com.alibaba.fastjson.JSON;
import com.gec.system.common.Result;
import com.gec.system.common.ResultCodeEnum;
import com.gec.system.config.Constant;
import com.gec.system.util.JwtHelper;
import com.gec.system.util.ResponseUtil;
import org.apache.bcel.Const;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.util.StringUtils;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class TokenAuthenticationFilter extends OncePerRequestFilter {
    private RedisTemplate redisTemplate;

    public TokenAuthenticationFilter(RedisTemplate redisTemplate){
        this.redisTemplate=redisTemplate;
    }

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws ServletException, IOException {
        String newUri = request.getRequestURI().replace(request.getContextPath(), "");
        logger.info("newUri:" + newUri);
        if(Constant.LONGIN_URL.equals(newUri)){
            logger.info("登录放行");
            chain.doFilter(request,response);
            return;
        }
        if(Constant.FILE_URL.equals(newUri)){
            logger.info("文件放行");
            chain.doFilter(request,response);
            return;
        }
        if(Constant.VIDEO_URL.equals(newUri)){
            logger.info("视频放行");
            chain.doFilter(request,response);
            return;
        }
//        if(Constant.LONGIN_URL.equals(newUri)|| Constant.FILE_URL.equals(newUri)||Constant.VIDEO_URL.equals(newUri)) {
//            logger.info("放行");
//            chain.doFilter(request, response);
//            return;
//        }
        UsernamePasswordAuthenticationToken authentication=getAuthentication(request);
        if(authentication != null){
            SecurityContextHolder.getContext().setAuthentication(authentication);
            chain.doFilter(request,response);
        }else{
            ResponseUtil.out(response, Result.build(null, ResultCodeEnum.PERMISSION));
        }
    }

    private UsernamePasswordAuthenticationToken getAuthentication(HttpServletRequest request) {
        String token = request.getHeader("token");
        logger.info(token);
        if(!StringUtils.isEmpty(token)){
            String username= JwtHelper.getUsername(token);
            logger.info("username:"+username);
            if(!StringUtils.isEmpty(username)){
                //return new UsernamePasswordAuthenticationToken(username,null, Collections.emptyList());
                String authoritiesString = (String) redisTemplate.opsForValue().get(username);
                List<Map> mapList= JSON.parseArray(authoritiesString,Map.class);
                List<SimpleGrantedAuthority> authorities=new ArrayList<>();
                for (Map map : mapList) {
                    authorities.add(new SimpleGrantedAuthority((String)map.get("authority")));
                }
                return new UsernamePasswordAuthenticationToken(username,null,authorities);
            }
        }
        return null;
    }
}
